import java.io.IOException;
import java.io.RandomAccessFile;

//Class containing operations of datatype cx
public class cx extends datatype implements read,write{

	public void write(String data, int length) throws IOException
	{
		//System.out.println("cx write called");
		String data2;
		StringBuffer stb =  new StringBuffer();
		RandomAccessFile fi= new RandomAccessFile(heap.f,"rw");
		fi.seek(heap.f.length()); 

		if(data.length()<length)
		{
			int remaining = length - data.length();

			for(int i=0; i<data.length();i++)
				stb.append(data.charAt(i));

			for(int j=0;j<remaining;j++)
				stb.append("~");

			data = stb.toString();

		}
		
		byte[] b = data.getBytes();
		fi.write(b);
		

		fi.close();
	}
	public String read(int length) throws IOException
	{
		char c;
		int count=0;
		String str="";
		RandomAccessFile fip= new RandomAccessFile(heap.f, "rw");
		fip.seek(heap.readpointer);
		while(count<length)
		{
			c=(char)fip.readByte();
			str+=c;
			count++;
			heap.readpointer+=1;
		}
		fip.close();
		return str;
	}
	public int compares(String record1,String record2) throws IOException
	{
		record1.trim();
		record2.trim();
		int result=record1.length()<record2.length()?record1.length():record2.length();
		for(int i=0;i<result;i++)
		{
			if(record1.charAt(i)<record2.charAt(i))
				return 2;
			else if(record1.charAt(i)>record2.charAt(i))
				return 1;	
		}
		if(record1.length()==record2.length())
			return 0;
		else if(record1.length()>record2.length())
			return 1;
		else 
			return 2;

	}
	public Object convert(String str)
	{
		return str;
	}
	public byte[] returnPrimitive(String str)
	{
		return (str==null)?null:str.getBytes();
	}
	
	public Object getPrimitive(byte [] data)
	{
		return new String(data);
	}
}
